package com.data.codegenerate.generate.util;

import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FreemarkerHelper {
    private static final Logger a = LoggerFactory.getLogger(FreemarkerHelper.class);

    public static Configuration a(List<File> list, String str, String str2) throws IOException {
        Configuration configuration = new Configuration();
        a.debug(" FileTemplateLoader[] size " + list.size());
        a.debug(" templateRootDirs templateName " + str2);
        FileTemplateLoader[] fileTemplateLoaderArr = new FileTemplateLoader[list.size()];
        for (int i = 0; i < list.size(); i++) {
            File file = list.get(i);
            a.debug(" FileTemplateLoader " + file.getAbsolutePath());
            fileTemplateLoaderArr[i] = new FileTemplateLoader(file);
        }
        configuration.setTemplateLoader(new MultiTemplateLoader(fileTemplateLoaderArr));
        configuration.setNumberFormat("###############");
        configuration.setBooleanFormat("true,false");
        configuration.setDefaultEncoding(str);
        return configuration;
    }

    public static List<String> a(String str, String str2) {
        String[] b = b(str, "\\/");
        ArrayList arrayList = new ArrayList();
        arrayList.add(str2);
        arrayList.add(File.separator + str2);
        String str3 = "";
        for (int i = 0; i < b.length; i++) {
            str3 = str3 + File.separator + b[i];
            arrayList.add(str3 + File.separator + str2);
        }
        return arrayList;
    }

    public static String[] b(String str, String str2) {
        if (str == null) {
            return new String[0];
        }
        StringTokenizer stringTokenizer = new StringTokenizer(str, str2);
        ArrayList arrayList = new ArrayList();
        while (stringTokenizer.hasMoreElements()) {
            arrayList.add(stringTokenizer.nextElement().toString());
        }
        return (String[]) arrayList.toArray(new String[arrayList.size()]);
    }

    public static String a(String str, Map<String, Object> map, Configuration configuration) {
        StringWriter stringWriter = new StringWriter();
        try {
            new Template("templateString...", new StringReader(str), configuration).process(map, stringWriter);
            return stringWriter.toString();
        } catch (Exception e) {
            throw new IllegalStateException("cannot process templateString:" + str + " cause:" + e, e);
        }
    }

    public static void a(Template template, Map<String, Object> map, File file, String str) throws IOException, TemplateException {
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), str));
        map.put("Format", new SimpleFormat());
        template.process(map, bufferedWriter);
        bufferedWriter.close();
    }
}
